class cart{
       // 获取总共商品的数量
       getTargetNumber() {
        let oNumber = document.getElementsByClassName("number");

        let oTargetNumber = document.querySelector(".target-number"); 
        let targetNumber = 0;
        for (let i = 0; i < oNumber.length; i++) {
          targetNumber += oNumber[i].innerHTML / 1;
        }
  
        oTargetNumber.innerHTML = targetNumber;
       }

        // 获取总价格
        getTargetMoney(){
            let oSum = document.querySelector(".sum");

            let oTargetMoney = document.querySelector(".target-money");

            let otargetMoney = 0;

            for (let i = 0; i < oSum.length; i++) {
              otargetMoney += oSum[i].innerHTML / 1;
            }
            oTargetMoney.innerHTML = oSum.innerHTML;
          }

          // 获取小计公式(数量*单价=小计)
          getsum(num, price) {
            return num * price;
          }
          // 加号的函数
          addGoods(btn){
              // 数量
              let oNumber = document.querySelector(".number");

              oNumber.innerHTML = Number(oNumber.innerHTML) + 1;
                 // 单价
                 let oMoney = document.querySelector(".money");

                   // 小计
                   let oSum =document.querySelector(".sum");

                   
                   oSum.innerHTML = this.getsum(oNumber.innerHTML, oMoney.innerHTML);

                   this.getTargetNumber();
                   this.getTargetMoney();
          }

           // 减号的函数
           minGoods(btn){
            let oNumber = document.querySelector(".number");
            if (oNumber.innerHTML > 0) {
              oNumber.innerHTML = Number(oNumber.innerHTML) - 1;
            }

            // 单价
            let oMoney = document.querySelector(".money");
            // 小计
            let oSum =document.querySelector(".sum");
            
    
          oSum.innerHTML = this.getsum(oNumber.innerHTML, oMoney.innerHTML);
    
          this.getTargetNumber();
          this.getTargetMoney();
           }

          // 删除的函数
          delGoods(btn) {
            let oCenter = btn.parentNode.parentNode;
            oCenter.remove();
            this.getTargetNumber();
            this.getTargetMoney();
          }



           // 验证的函数
           eventBind() {
            let oBtns = document.getElementsByTagName("button");
            let that = this;
            for (let i = 0; i < oBtns.length; i++) {
              if (i % 2 == 0) {
                oBtns[i].onclick = function () {
                  that.minGoods(this);
                };
              } else {
                oBtns[i].onclick = function () {
                  that.addGoods(this);
                };
              }
            }
            let oInputs = document.getElementsByTagName("input");
      
            for (let i = 0; i < oInputs.length; i++) {
              oInputs[i].onclick = function () {
                that.delGoods(this);
              };
            }
          }
        }

        let c = new cart();
        c.eventBind();
